// mock/system.ts
import LogSingleton from './log';
let mentList = [
	{
		id: 1,
		path: '/home',
		name: 'home',
		component: 'home/index',
		meta: {
			title: '首页',
			isLink: '',
			isHide: false,
			isKeepAlive: false,
			isAffix: true,
			isIframe: false,
			roles: ['admin', 'common'],
			icon: 'iconfont icon-shouye',
		},
	},
	{
		id: 2,
		path: '/system',
		name: 'system',
		component: 'layout/routerView/parent',
		redirect: '/system/menu',
		meta: {
			title: '系统设置',
			isLink: '',
			isHide: false,
			isKeepAlive: false,
			isAffix: false,
			isIframe: false,
			roles: ['admin'],
			icon: 'iconfont icon-xitongshezhi',
		},
		children: [
			{
				pid: 2,
				id: 3,
				path: '/system/menu',
				name: 'systemMenu',
				component: 'system/menu/index',
				meta: {
					title: '菜单管理',
					isLink: '',
					isHide: false,
					isKeepAlive: false,
					isAffix: false,
					isIframe: false,
					roles: ['admin'],
					icon: 'iconfont icon-caidan',
				},
			},
			{
				pid: 2,
				id: 4,
				path: '/system/user',
				name: 'systemUser',
				component: 'system/user/index',
				meta: {
					title: '用户管理',
					isLink: '',
					isHide: false,
					isKeepAlive: false,
					isAffix: false,
					isIframe: false,
					roles: ['admin'],
					icon: 'iconfont icon-icon-',
				},
			},
			{
				pid: 2,
				id: 5,
				path: '/system/role',
				name: 'systemRole',
				component: 'system/role/index',
				meta: {
					title: '角色管理',
					isLink: '',
					isHide: false,
					isKeepAlive: false,
					isAffix: false,
					isIframe: false,
					roles: ['admin'],
					icon: 'iconfont icon-icon-',
				},
			},
			{
				pid: 2,
				id: 6,
				path: '/system/logData',
				name: 'systemLog',
				component: 'system/logData/index',
				meta: {
					title: '日志管理',
					isLink: '',
					isHide: false,
					isKeepAlive: false,
					isAffix: false,
					isIframe: false,
					roles: ['admin'],
					icon: 'iconfont icon-icon-',
				},
			},
		],
	},
];
let numId = 7;

//通过pid找对应菜单 新增
const addLabelToParents = (routes: any, pid: any, body: any) => {
	// 遍历每个路由对象
	routes.forEach((route: any) => {
		if (route.id == pid) {
			// 找到了对应的父级路由，将新的路由对象添加到父级路由的 children 数组中
			if (!route.children) {
				route.children = [];
			}
			route.children.push(body);
			return;
		} else if (route.children && route.children.length > 0) {
			// 递归调用 addLabelToParents 函数，在子路由中查找对应的父级路由
			addLabelToParents(route.children, pid, body);
		}
	});

	return routes;
};
//通过pid找对应菜单 编辑
const editLabelToParents = (routes: any, body: any) => {
	// 遍历每个路由对象
	routes.forEach((route: any) => {
		if (route.id == body.id) {
			// 找到了对应的路由，直接覆盖 同时把子集中的路由复制回去
			let child = route.children || [];
			Object.assign(route, body);
			route.children = child;
			return;
		} else if (route.children && route.children.length > 0) {
			// 递归调用 editLabelToParents 函数，在子路由中查找
			editLabelToParents(route.children, body);
		}
	});

	return routes;
};


const recursiveRemove = (nodes:any,id:any)=>{
	for (let i = nodes.length - 1; i >= 0; i--) {
		if (nodes[i].id === id) {
			nodes.splice(i, 1); 
			return true; 
		}
		if (nodes[i].children && recursiveRemove(nodes[i].children,id)) {
			return true; 
		}
	}
	return false; 
}

export default [
	// 获取用户菜单 简单查询
	{
		url: '/api/system/getUserMenuList',
		method: 'post',
		response: ({ body }: any) => {
			let newList: any = [];
			if (body.menuName) {
				newList = mentList.filter((item) => item.meta.title.indexOf(body.menuName) != -1);
			} else {
				newList = mentList;
			}

			return {
				code: 200,
				data: newList,
			};
		},
	},
	//新增/编辑菜单
	{
		url: '/api/system/addMenu',
		method: 'post',
		response: (data: any) => {
			
			let body = data.body
			// console.log(body);
			//先判断pid 根据pid没有pid默认0 ， 0 代表最外层
			if (body.pid) {
				//有pid 在子集里新增 自循环从树种找到对应pid 然后新增
				if (body.id) {
					//编辑
					LogSingleton.addToLogData({
						logName:'编辑菜单',
						url: data.url,
						data: data.body,
					})
					editLabelToParents(mentList, body);
				} else {
					LogSingleton.addToLogData({
						logName:'新增菜单',
						url: data.url,
						data: data.body,
					})
					body.id = numId;
					numId++;
					addLabelToParents(mentList, body.pid, body);
				}

				//有pid 在自己里编辑
			} else {
				//没有 没有证明是最外层 看是新增还是编辑
				if (body.id) {
					//编辑 找到对应菜单 然后替换
					LogSingleton.addToLogData({
						logName:'编辑菜单',
						url: data.url,
						data: data.body,
					})
					let index = mentList.findIndex((item) => item.id == body.id);
					mentList[index] = body;
				} else {
					LogSingleton.addToLogData({
						logName:'新增菜单',
						url: data.url,
						data: data.body,
					})
					//新增
					body.id = numId;
					numId++;
					mentList.push(body);
				}
			}

			return {
				code: 200,
				data: '操作成功',
			};
		},
	},
	//删除菜单
	{
		url: '/api/system/delMenu',
		method: 'post',
		response: (data: any) => {
			LogSingleton.addToLogData({
				logName:'删除菜单',
				url: data.url,
				data: data.body,
			})
			let body = data.body
			recursiveRemove(mentList,body.id)

			return {
				code: 200,
				data: '操作成功',
			};
		},
	},
	//获取日志
	{
		url: '/api/system/sysLogList',
		method: 'post',
		response: (request: any) => {
			console.log(LogSingleton.getLogData,'当前日志');
			
			return {
				code: 200,
				data: LogSingleton.getLogData,
			};
		},
	}
];
